package com.gw.booking.service;

import com.gw.booking.common.ApiException;
import com.gw.booking.common.MessageCode;
import com.gw.booking.common.Util;
import com.gw.booking.dto.PageDto;
import com.gw.booking.dto.backend.MerchantListDtoOut;
import com.gw.booking.dto.backend.MerchantTypeDto;
import com.gw.booking.dto.backend.MerchantTypeListDtoOut;
import com.gw.booking.entity.LoginToken;
import com.gw.booking.entity.Logs;
import com.gw.booking.entity.MerchantType;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MerchantTypeService {
    @Autowired
    private SessionFactory sessionFactory;
    @Autowired
    private LogService logService;
    @Autowired
    private MerchantService merchantService;

    public List<MerchantTypeListDtoOut> list() {
        Session session = sessionFactory.getCurrentSession();
        String sql = " select id, type_name as typeName, order_time_num as orderTimeNum from merchant_type ";
        String sqlOrder = " order by id desc ";
        List<MerchantTypeListDtoOut> items = session.createSQLQuery(sql + sqlOrder)
                .setResultTransformer(Transformers.aliasToBean(MerchantTypeListDtoOut.class))
                .list();
        return items;
    }

    public void txAdd(LoginToken loginToken, MerchantTypeDto merchantTypeDto) {
        Session session = sessionFactory.getCurrentSession();
        MerchantType merchantType = new MerchantType();
        merchantType.setTypeName(merchantTypeDto.getTypeName());
        merchantType.setOrderTimeNum(merchantTypeDto.getOrderTimeNum());
        session.save(merchantType);
        session.flush();
        logService.txAddLog(Logs.LogType.add, loginToken.getUserId(), "新增商家分类：" + merchantType.getId());
    }

    public void txDelete(LoginToken loginToken, Integer typeId) {
        Session session = sessionFactory.getCurrentSession();
        merchantService.checkMerchantType(session, typeId);
        MerchantType merchantType = (MerchantType) session.get(MerchantType.class, typeId);
        if (merchantType == null) {
            throw new ApiException(MessageCode.CODE_MERCHANT_TYPE_NOT_EXISTS);
        }
        session.delete(merchantType);
        session.flush();
        logService.txAddLog(Logs.LogType.delete, loginToken.getUserId(), "删除商家分类：" + merchantType.getId());
    }

    public MerchantType sureMerchantType(Session session, Integer typeId) {
        if (typeId == null) {
            throw new ApiException(MessageCode.CODE_MERCHANT_TYPE_NOT_EXISTS);
        }
        MerchantType merchantType = (MerchantType) session.get(MerchantType.class, typeId);
        if (merchantType == null) {
            throw new ApiException(MessageCode.CODE_MERCHANT_TYPE_NOT_EXISTS);
        }
        return merchantType;
    }
}
